package xyz.zhouxf.rpc.transport.netty.client;

import io.netty.util.concurrent.Promise;
import xyz.zhouxf.rpc.entity.RpcRequest;
import xyz.zhouxf.rpc.entity.RpcResponse;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhouxf
 * @date 2022/10/5
 */
public class WaitRequests {
    private static final ConcurrentHashMap<String, Promise<RpcResponse>> requests = new ConcurrentHashMap<>();

    public void put(String requestId, Promise<RpcResponse> promise) {
        requests.put(requestId, promise);
    }

    public void remove(String requestId) {
        requests.remove(requestId);
    }

    public void result(RpcResponse response) {
        Promise<RpcResponse> promise = requests.remove(response.getRequestId());
        if(promise != null) {
            promise.setSuccess(response);
        }
    }


}
